package com.moon.game.server;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.jboss.netty.channel.MessageEvent;

import com.moon.game.command.RequestSession;

public class SessionPool {
    public static SessionPool inst = new SessionPool();
    
    private int GLOBAL_SESSION_ID = 0;
    // 线程间传送数据的队列,来自客户端的请求或来自网络server的请求都要通过这个队列交给LogicThread处理
    private LinkedBlockingQueue<UniData> exchangeQueue = new LinkedBlockingQueue<UniData>();
    // session集合,保存处理中的session对象
    private ConcurrentHashMap<Integer, RequestSession> reqSessions = new ConcurrentHashMap<Integer, RequestSession>();
    // 超时检测队列,每个发到网络server的包都会在这里增加一个
    private ConcurrentLinkedQueue<UniData> timeoutQueue = new ConcurrentLinkedQueue<UniData>();
    public void init() {
    }
    
    /**
     * 执行队列数据
     * 
     * @author sky
     * 
     */
    public static class UniData {
        public static int FROM_CLIENT = 0;
        public static int FROM_TIMER = 1;
        public static int FROM_NET = 2;
        public int sessionId = 0;
        public int from = 0;
        public int seq = 0;
        public int status = 0;
        public long pushTime = 0;
        public MessageEvent event = null;
        public Object data = null;
        public int serverCmd = 0;


		public UniData() {
        }

        public UniData(int from, int sessionId, Object data) {
            this.from = from;
            this.data = data;
            this.sessionId = sessionId;
        }

        /**
         * session对象的数据
         * 
         * @param from
         * @param data
         * @param messageEvent
         */
        public UniData(int from, Object data, MessageEvent event) {
            this.from = from;
            this.data = data;
            this.event = event;
        }
        
        /**
         * 设置超时重发
         * 
         * @param from
         * @param sessionId
         * @param channel
         * @param serverCmd
         * @param status
         */
        public UniData(int from, int sessionId, MessageEvent event, int serverCmd, int status) {
            this.pushTime = System.currentTimeMillis();
            this.from = from;
            this.event = event;
            this.sessionId = sessionId;
            this.serverCmd = serverCmd;
            this.status = status;
        }
    }


    public synchronized int getSessionId(){
    	return ++GLOBAL_SESSION_ID;
    }
    
    /**
     * 设置执行数据
     * @param from
     * @param data
     * @param channel
     * @throws InterruptedException
     */
    public void addData(UniData uniData) {
        exchangeQueue.add(uniData);
    }

    public LinkedBlockingQueue<UniData> getExchangeQueue() {
        return exchangeQueue;
    }

    /**
     * Session
     */
    public void addReqSession(RequestSession session){
    	reqSessions.put(session.getSessionId(), session);
    }
    
    /**
     * 删除队列中的请求
     * 
     * @param sessionId
     */
    public void delReqSession(int sessionId){
    	reqSessions.remove(sessionId);
    }
    
    public RequestSession getReqSession(int sessionId){
    	return reqSessions.get(sessionId);
    }
    
    /**
     * Timeout
     */
    public void addTimeout(UniData uniData){
    	timeoutQueue.add(uniData);
    }
    
    public ConcurrentLinkedQueue<UniData> getTimeoutQueue() {
        return timeoutQueue;
    } 
}
